package com.blog.result;

import lombok.Data;

import java.io.Serializable;

/**
 * 后端统一返回格式
 */
@Data
public class Result<T> implements Serializable {

    private Integer code;//编码：1表示成功，0或者其他数表示失败
    private String msg; //错误或者提示信息
    private T data; // 返回给前端的数据

    //请求成功但没有返回数据
    public static <T> Result<T> success(){
        Result<T> result = new Result<T>();
        result.code = 1;
        return result;
    }

    //请求成功且有数据返回
    public static <T> Result<T> success(T data){
        Result<T> result =new Result<T>();
        result.data = data;
        result.code = 1;
        return result;
    }

    //请求失败
    public static <T> Result<T> error(String msg){
        Result<T> result = new Result<>();
        result.code = 0;
        result.msg = msg;
        return result;
    }
}
